import "../public/index";
import url from "../data/url";
import request from "../data/request";
import { tableConfig, header } from "../data/config";
// 引入验证规则，只能输入字符和数字
import { deviceCode } from "../data/validate";
// 这个更改规则不变化之前的规则
const cusId = deviceCode;
import { getUrlParam } from "../data/util";
import store from "store";

const base = url.base;
const addPos = "/admin/equipmentInfo/doAddPos.json";

const app = new Vue({
  el: "#form",
  data: {
    equipType: "pos",
    identifierCode: null,
    identifier: null,
    propertyArea: null,
    type: null,
    propertyAddress: null,
    typeList: [
      { val: "2", text: "拉卡拉POS" },
      { val: "3", text: "银商POS" }
    ],
    propertyAreaList: [
      { val: "hd", text: "华东" },
      { val: "hx", text: "华西" },
      { val: "hb", text: "华北" },
      { val: "hn", text: "华南" },
      { val: "hz", text: "华中" }
    ]
  },
  created() {
    this.valid();
  },
  methods: {
    // 获取页面类型pos还是一体机
    getPageType() {
      const type = getUrlParam("type");
      this.type = type;
      let equipType;
      switch (type) {
        case "pos":
          equipType = "pos";
          break;
        case "ytj":
          equipType = "一体机";
          break;
      }
      this.equipType = equipType;
    },
    valid() {
      const _form = this;
      let data;

      data = $("#form").serializeObject();

      let form_valid,
        $form = $("#form");
        

      $.validator.setDefaults({
        // 只能放在validate规则配置之前

        // 提交事件
        submitHandler: () => {
          console.log("submit!");

          let loading_modal = layer.msg("提交中...", {
            icon: 16,
            time: 0
          });

          // console.log("75 data:", data);
          $.ajax({
            type: "POST",
            url: base + addPos,
            data: data,
            dataType: "json",
            headers: {
              myToken: Cookies.get("myToken")
            }
          })
            .done(function(res) {
              console.log("res====>", res);

              if (res.code === -1) {
                layer.msg(res.message, {
                  icon: 5
                });

                return;
              }

              layer.msg(`添加设备成功`, {
                icon: 6
              });
              setTimeout(() => {
                window.location = "./index.html?type=pos";
              }, 1000);
            })
            .fail(function() {
              layer.msg("添加失败", {
                icon: 5
              });
            })
            .always(function(res) {
              // layer.close(loading_modal);
              // form.isLock = false
            });
        }
      });

      let validOrderFailureTime;
      validOrderFailureTime = _form.isApp;
      // 验证
      form_valid = $form.validate({
        errorPlacement: function(error, element) {
          error.appendTo(element.parents(".col-xs-24"));
        },
        rules: {
          identifier: cusId,
          propertyPayment: {
            required: true
          },
          
        }
      });
    },
    // 确认信息
    rUSure2Submit() {
      const device = this.equipType;
      const deviceCode = this.identifierCode;
      if (!app.$data.type) {
        layer.open({
          content: `请完善支付类型`
        })
        return
      }
      if (!deviceCode) {
        layer.open({
          content: `请完善设备号`
        })
        return
      }
      
      const submitForm = layer.confirm(
        "",
        {
          title: "请确认设备新增信息",
          content: `您正在新增<strong style="color:red">${device}</strong>  <strong style="color:red" >${
            deviceCode ? deviceCode : ""
          }</strong> 请确认是否添加该设备`,
          btn: ["是", "否"] //按钮
        },
        () => {
          app.valid();
          app.submitForm();
        },
        () => {
          layer.close(submitForm);
        }
      );
    },
    submitForm() {
      $("#form").submit();
    }
  }
});
